package org.jeecg.modules.rooms.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.Enum.CouponEnums;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.rooms.DTO.CesRoomLayoutMemberInfoPriceDto;
import org.jeecg.modules.rooms.DTO.CesRoomLayoutPriceDto;
import org.jeecg.modules.rooms.DTO.CesRoomLayoutPriceSearchDto;
import org.jeecg.modules.rooms.entity.CesRoomLayoutMemberPrice;
import org.jeecg.modules.rooms.entity.CesRoomLayoutPrice;
import org.jeecg.modules.rooms.mapper.CesRoomLayoutPriceMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fendo
 * @since 2023-03-06
 */
@Service
public class CesRoomLayoutPriceServiceImpl extends ServiceImpl<CesRoomLayoutPriceMapper, CesRoomLayoutPrice> implements IService<CesRoomLayoutPrice> {
    @Resource
    private CesRoomLayoutPriceMapper roomLayoutPriceMapper;

    @Resource
    private CesRoomLayoutMemberPriceServiceImpl memberPriceService;


    /**
     * 查询某家酒店下某个房型的房间价格管理数据
     * @param searchDto
     * @return
     */
    public Result list(CesRoomLayoutPriceSearchDto searchDto){
        QueryWrapper<CesRoomLayoutPrice> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq(CesRoomLayoutPrice.HOTELID,searchDto.getHotelId());
        queryWrapper.eq(CesRoomLayoutPrice.LAYOUTID,searchDto.getRoomLayoutId());
        if(searchDto.getType() != null && searchDto.getType() > 0) queryWrapper.eq(CesRoomLayoutPrice.TYPE,searchDto.getType().intValue());
        queryWrapper.eq(CesRoomLayoutPrice.INVALID,false);

        queryWrapper.orderByDesc(CesRoomLayoutPrice.CREATEAT);
        IPage<CesRoomLayoutPrice>  dataPage = roomLayoutPriceMapper.selectPage(new Page<>(searchDto.getPageNo().intValue(),searchDto.getPageSize().intValue()),queryWrapper);
        Map<String,Object> map = new HashedMap();
        map.put("records",dataPage.getRecords());
        map.put("total",dataPage.getTotal());
        return Result.OK(map);
    }


    /**
     * 创建房型价格
     * @param roomLayoutPriceDto
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Result createRoomLayoutPrice(CesRoomLayoutPriceDto roomLayoutPriceDto){
         CesRoomLayoutPrice roomLayoutPrice = new CesRoomLayoutPrice();
         roomLayoutPrice.setHotelId(roomLayoutPriceDto.getHotelId());
         roomLayoutPrice.setLayoutId(roomLayoutPriceDto.getLayoutId());
         roomLayoutPrice.setName(roomLayoutPriceDto.getName());
         roomLayoutPrice.setOprice(roomLayoutPriceDto.getOprice());//原价
         roomLayoutPrice.setIntegral(roomLayoutPriceDto.getIntegral().intValue());//积分
         roomLayoutPrice.setIsVip(roomLayoutPriceDto.getIsVip().intValue() == 1 ? true:false);
         roomLayoutPrice.setNum(roomLayoutPriceDto.getNum());
         roomLayoutPrice.setPrice(roomLayoutPriceDto.getPrice());//价格
         roomLayoutPrice.setPrepay(roomLayoutPriceDto.getPrepay().intValue() == 1 ? true:false);//是否预支付
         roomLayoutPrice.setCreateAt(LocalDateTime.now());
         roomLayoutPrice.setUpdateAt(LocalDateTime.now());
         roomLayoutPrice.setType(roomLayoutPriceDto.getType());
         roomLayoutPrice.setInvalid(false);
         if(org.jeecg.modules.rooms.Enum.CouponEnums.RoomLayoutPriceTypeEnum.HOUR.code() == roomLayoutPriceDto.getType().intValue()){
             roomLayoutPrice.setStartDate(roomLayoutPriceDto.getStartDate());
             roomLayoutPrice.setEndDate(roomLayoutPriceDto.getEndDate());
             roomLayoutPrice.setSellerId(roomLayoutPriceDto.getSellerId());
         }
        roomLayoutPriceMapper.insert(roomLayoutPrice);
        String parentId = roomLayoutPrice.getId();
        if(roomLayoutPriceDto.getIsVip().intValue() == CouponEnums.TRUEORFALSE.TRUE.code()){
            //插入 会员折扣数据
            List<CesRoomLayoutMemberInfoPriceDto> memberPriceEntities = roomLayoutPriceDto.getMemberPriceEntities();
            List<CesRoomLayoutMemberPrice> memberPriceList = new ArrayList<>();
            memberPriceEntities.forEach(v -> {
                CesRoomLayoutMemberPrice memberPrice = new CesRoomLayoutMemberPrice();
                memberPrice.setHotelId(roomLayoutPriceDto.getHotelId());
                memberPrice.setLayoutId(roomLayoutPriceDto.getLayoutId());
                memberPrice.setLevelId(v.getLevelId());
                memberPrice.setParentId(parentId);
                memberPrice.setLdiscount(v.getLdiscount());
                memberPrice.setDiscount(v.getDiscount());
                memberPrice.setCreateAt(LocalDateTime.now());
                memberPrice.setUpdateAt(LocalDateTime.now());
                memberPrice.setInvalid(false);
                memberPriceList.add(memberPrice);
            });
            memberPriceService.saveBatch(memberPriceList);
        }
        return Result.ok("创建成功");
    }


    /**
     * 修改房型价格数据
     * @param roomLayoutPriceDto
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Result modifyRoomLayoutPrice(CesRoomLayoutPriceDto roomLayoutPriceDto){
        CesRoomLayoutPrice roomLayoutPrice = roomLayoutPriceMapper.selectById(roomLayoutPriceDto.getId());
        if(ObjectUtils.isEmpty(roomLayoutPrice)) return Result.error("价格管理数据未找到!");

        BeanUtil.copyProperties(roomLayoutPriceDto,roomLayoutPrice, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        roomLayoutPrice.setIsVip(roomLayoutPriceDto.getIsVip().intValue() == 1? true:false);
        roomLayoutPrice.setPrepay(roomLayoutPriceDto.getPrepay().intValue() == 1 ? true:false);//是否预支付
        roomLayoutPrice.setUpdateAt(LocalDateTime.now());
        roomLayoutPriceMapper.updateById(roomLayoutPrice);
        if(roomLayoutPriceDto.getIsVip().intValue() == CouponEnums.TRUEORFALSE.TRUE.code()){
            //修改 或者创建 会员折扣数据
            List<CesRoomLayoutMemberInfoPriceDto> memberPriceEntities = roomLayoutPriceDto.getMemberPriceEntities();

            List<CesRoomLayoutMemberPrice> memberPriceList = new ArrayList<>();
            memberPriceEntities.forEach(v -> {
                CesRoomLayoutMemberPrice memberPrice = new CesRoomLayoutMemberPrice();
                if(!StringUtils.isBlank(v.getId())){
                    memberPrice.setId(v.getId());
                }else{
                    memberPrice.setHotelId(roomLayoutPriceDto.getHotelId());
                    memberPrice.setLayoutId(roomLayoutPriceDto.getLayoutId());
                    memberPrice.setParentId(roomLayoutPrice.getId());
                    memberPrice.setLevelId(v.getLevelId());
                    memberPrice.setInvalid(false);
                    memberPrice.setCreateAt(LocalDateTime.now());
                }
                memberPrice.setLdiscount(v.getLdiscount());
                memberPrice.setDiscount(v.getDiscount());
                memberPrice.setUpdateAt(LocalDateTime.now());
                memberPriceList.add(memberPrice);
            });
            memberPriceService.saveOrUpdateBatch(memberPriceList);
        }else{
            //查询现有会员等级价格
            //TODO:删除之前的会员等级价格
        }
        return Result.ok("修改成功!");
    }


    /**
     * 删除房型价格数据
     * @param roomLayoutPriceDto
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Result deleteRoomLayoutPrice(CesRoomLayoutPriceDto roomLayoutPriceDto){
        CesRoomLayoutPrice roomLayoutPrice = roomLayoutPriceMapper.selectById(roomLayoutPriceDto.getId());
        if(ObjectUtils.isEmpty(roomLayoutPrice)) return Result.error("删除失败,价格管理数据未找到!");
        roomLayoutPrice.setUpdateAt(LocalDateTime.now());
        roomLayoutPrice.setInvalid(true);
        roomLayoutPriceMapper.updateById(roomLayoutPrice);
        //删除会员等级折扣数据
        if(roomLayoutPrice.getIsVip()){
            List<CesRoomLayoutMemberPrice> data = memberPriceService.getListByParentId(roomLayoutPrice.getId());
            data.forEach(v -> {
                v.setInvalid(true);
                v.setUpdateAt(LocalDateTime.now());
            });
            memberPriceService.updateBatchById(data);
        }
        return Result.ok("删除成功!");
    }








}
